﻿package com.riatimes.net.remoting
{

	import com.adobe.serialization.json.JSON;
	import com.riatimes.mvc.model.vo.RemoteDataParamVO;
	import com.riatimes.net.localconnection.LocalConnectionService;
	import com.riatimes.util.Logger;
	import com.riatimes.util.Reflection;
	
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	import flash.net.FileReference;
	import flash.net.ObjectEncoding;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.Font;
	import flash.utils.Dictionary;
	
	import org.bytearray.remoting.PendingCall;
	import org.bytearray.remoting.Service;
	import org.bytearray.remoting.events.FaultEvent;
	import org.bytearray.remoting.events.ResultEvent;
	import org.puremvc.as3.patterns.observer.Notifier;

	/**
	 * Remoting 服务
	 * @author Nick Wong
	 */
	public class RemotingService extends Notifier
	{
		private var service:Service;
		private static var _instance:RemotingService;

		/**
		 * 获取实例
		 * @return
		 */
		public static function getInstance():RemotingService
		{
			if (!_instance)
				_instance=new RemotingService();
			return _instance;
		}

		/**
		 * 构造函数
		 */
		public function RemotingService()
		{
//			service=new Service("", ModelLocator.gateway, ObjectEncoding.AMF3); //初始化remoting服务
//			service.addEventListener(NetStatusEvent.NET_STATUS, handleEvent);
//			if (ModelLocator.isLocalConection)
//				LocalConnectionService.getInstance()["remoting"]=__localconnection; //remoting调试
		}


		private function handleEvent(e:NetStatusEvent):void
		{
			//如果是错误级别，则打印错误信息
			if (e.info.level == "error")
			{
				Logger.logError(e.info.code);
			}
		}

		/**
		 *remoting 调试用
		 * @param name
		 * @param obj
		 *
		 */
		private function __localconnection(name:String, obj:Object):void
		{
//			var rEvent:ResultEvent=new ResultEvent(ResultEvent.RESULT, null);
//			rEvent.result=obj;
//			var vo:RemoteDataParamVO=new RemoteDataParamVO("", name);
//			__successHandle(rEvent, vo);
		}


		/**
		 * 发送命令
		 * @param vo
		 */
		public function callMethod(vo:RemoteDataParamVO):void
		{
			var rpc:PendingCall;
			Logger.logInfo("Send:");
			Logger.logObject({"method": vo.method, "callbackEvent": vo.callbackEvent, "params": vo.params});

			rpc=service.callMethod(vo.method, vo.params); //调用remoting服务

			rpc.addEventListener(ResultEvent.RESULT, function(e:ResultEvent):void
			{
				__successHandle(e, vo);
			});
			rpc.addEventListener(FaultEvent.FAULT, function(e:FaultEvent):void
			{
				Logger.logObject(e.fault);
					//__successHandle(e, vo);
			})
		}


		/**
		 * rpc成功回调函数
		 * @param e
		 * @param vo remoting参数
		 */
		public function __successHandle(e:ResultEvent=null, vo:RemoteDataParamVO=null):void
		{

			/*var dataJson:String=JSON.encode(e.result);
			LocalConnectionService.getInstance().send(vo.callbackEvent,dataJson);
*/

			var code:String=e.result.s; //服务端返回的状态编号

			//如果编号不为200，则有错误发生，提示用户错误信息对话框
			if (code != "200")
			{
				//showInfo(e.result.m);		
				Logger.logError(code + ":" + e.result.m);
				
				return;
			}

			var data:*=e.result.d; //服务端返回的数据对象
			Logger.logInfo("Received:");
//			if (vo.callbackEvent != ApplicationFacade.EVENT_CF_DATA_INIT)
//				Logger.logObject(data);
			var isAutoSendBackEvent:Boolean=true; //是否发送vo里的回调事件
			var returnData:*=data; //需要随回调事件发送出去的对象
			
			//根据回调事件筛选
			switch (vo.callbackEvent)
			{
				

			}

			//如果设置自动转发通知为true，则发送回调事件通知，并把returnData做为body发送
			if (isAutoSendBackEvent)
			{
				sendNotification(vo.callbackEvent, returnData);
			}
		}

	}

}